<template>
  <el-row>
    <el-card>
      <el-form ref="searchForm" :inline="true" :model="searchForm" class="demo-form-inline">
        <el-form-item :label="tableText['updateTime']">
          <el-date-picker
            v-model="time"
            type="daterange"
            range-separator="~"
            :start-placeholder="$t('common.startDate')"
            :end-placeholder="$t('common.endDate')"
          />
        </el-form-item>
        <el-form-item :label="tableText['creditStatus']">
          <el-select v-model="searchForm.creditStatus">
            <el-option
              v-for="item in tableText['status']"
              :key="item.value"
              :label="item.desc"
              :value="item.value"
            />
          </el-select>
        </el-form-item>
        <el-form-item :label="tableText['phone']">
          <el-input v-model="searchForm.phoneNumber" clearable :placeholder="$t('common.input')" :maxlength="50" />
        </el-form-item>
        <el-form-item :label="tableText['ktp']">
          <el-input v-model="searchForm.ktp" clearable :placeholder="$t('common.input')" :maxlength="50" />
        </el-form-item>
        <el-row>
          <el-button type="primary" @click="search">
            {{ $t('common.search') }}
          </el-button>
          <el-button @click="reset">
            {{ $t('common.reset') }}
          </el-button>
          <el-button :disabled="!(totalNumber > 0)" @click="exportList">
            {{ $t('common.ex') }}
          </el-button>
        </el-row>
      </el-form>
    </el-card>
    <el-card>
      <el-row>
        <el-button @click="downTemplate">
          {{ $t('blackList.down') }}
        </el-button>
        <el-button @click="importList">
          {{ $t('blackList.import') }}
          <input id="file" type="file" accept=".xls,.xlsx" style="display: none" @change="upLoad">
        </el-button>
      </el-row>
      <el-table
        v-loading="loading"
        :header-cell-style="{background:'#eef1f6',color:'#606266'}"
        :data="tableData"
        border
        :stripe="true"
        style="width: 100%;margin-top: 10px"
      >
        <el-table-column
          prop="phoneNumber"
          :label="tableText['phone']"
          min-width="120"
        />
        <el-table-column
          prop="ktp"
          :label="tableText['ktp']"
          min-width="120"
        />
        <el-table-column
          prop="creditStatusStr"
          :label="tableText['creditStatus']"
          min-width="100"
        />
        <el-table-column
          prop="modifyTimeStr"
          :label="tableText['updateTime']"
          min-width="150"
        />
        <el-table-column
          prop="modifyUserIdName"
          :label="tableText['updater']"
          min-width="100"
        />
        <el-table-column
          :label="$t('common.operate.name')"
          min-width="200"
        >
          <template slot-scope="scope">
            <el-button v-if="scope.row['creditStatus'] === 1" size="small" @click="updateItem(scope.row, 1)">{{ tableText['markNormal'] }}</el-button>
            <el-button v-else size="small" @click="updateItem(scope.row, 0)">{{ tableText['markBlackList'] }}</el-button>
            <el-button type="danger" size="small" @click="deleteItem(scope.row)">{{ $t('common.operate.delete') }}</el-button>
          </template>
        </el-table-column>
      </el-table>
      <div class="block">
        <el-pagination
          :current-page="searchForm.currentPage"
          :page-sizes="[10, 20, 30, 40, 50]"
          :page-size="searchForm.pageSize"
          :total="totalNumber"
          layout="total, sizes, prev, pager, next, jumper"
          @size-change="handleSizeChange"
          @current-change="handleCurrentChange"
        />
      </div>
    </el-card>
    <delete-dialog ref="delete-dialog" :delete-state="deleteState" :loading="deleteLoading" @change-delete="changeDelete" @delete-this="deleteThis" />
  </el-row>
</template>

<script>
import { getBlackList, templateBlackList, exportBlackList, importBlackList, updateBlackList, deleteBlackList } from '@/api/risk'
import { formatDate } from '@/utils/method'
import deleteDialog from '@/components/delete/index.vue'
export default {
  name: 'BlackList',
  components: {
    deleteDialog
  },
  data() {
    return {
      deleteState: false,
      deleteLoading: false,
      editDialog: false,
      loading: true,
      tableData: [],
      allStage: [],
      allFirm: [],
      allGroup: [],
      time: '',
      tableText: {},
      id: null,
      totalNumber: 0,
      searchForm: this.params()
    }
  },
  mounted() {
    this.tableText = this.$t('blackList')
    this.getData()
  },
  methods: {
    params() {
      return {
        currentPage: 1,
        pageSize: 10,
        createTimeStart: null,
        createTimeEnd: null,
        creditStatus: null,
        phoneNumber: null,
        ktp: null
      }
    },
    search() {
      this.searchForm.currentPage = 1
      this.getData()
    },
    reset() {
      this.searchForm = this.params()
      this.time = ''
      this.getData()
    },
    handleSizeChange(val) {
      this.searchForm.pageSize = val
      this.getData()
    },
    handleCurrentChange(val) {
      this.searchForm.currentPage = val
      this.getData()
    },
    getData() {
      this.loading = true
      getBlackList(this.searchData())
        .then(res => {
          this.loading = false
          if (res.success) {
            this.tableData = res.data
            this.totalNumber = res.page ? res.page['totalNumber'] : 0
          } else {
            this.$message.error(res.message)
          }
        })
        .catch(err => {
          this.loading = false
          this.$message.error(err.message)
        })
    },
    searchData() {
      const data = this.searchForm
      if (this.time && this.time.length > 1) {
        data['createTimeStart'] = formatDate(new Date(this.time[0]), 'yyyy-MM-dd') + ' 00:00:00'
        data['createTimeEnd'] = formatDate(new Date(this.time[1]), 'yyyy-MM-dd') + ' 23:59:59'
      } else {
        data['createTimeStart'] = null
        data['createTimeEnd'] = null
      }
      return data
    },
    exportList() {
      exportBlackList(this.searchData())
    },
    setId(data) {
      const index = this.tableData.findIndex(fruit => fruit === data)
      return (this.searchForm.currentPage - 1) * 10 + index + 1
    },
    downTemplate() {
      templateBlackList()
    },
    importList() {
      document.getElementById('file').click()
    },
    updateItem(item, state) {
      this.loading = true
      const data = {
        creditStatus: state,
        id: item['id']
      }
      updateBlackList(data)
        .then(res => {
          if (res.success) {
            this.$message.success(this.$t('common.tip.editSuccess'))
            this.getData()
          } else {
            this.loading = false
            this.$message.error(res.message)
          }
        })
        .catch(err => {
          this.loading = false
          this.$message.error(err.message)
        })
    },
    upLoad() {
      this.loading = true
      const file = document.getElementById('file')['files'][0]
      const formData = new FormData()
      formData.append('file', file)
      importBlackList(formData)
        .then(res => {
          this.loading = false
          if (res.success) {
            this.$message.success(this.$t('common.tip.importSuccess'))
            this.getData()
          } else {
            this.$message.error(res.message)
          }
        })
        .catch(err => {
          this.loading = false
          this.$message.error(err.message)
        })
    },
    deleteItem(data) {
      this.id = data.id
      this.changeDelete(true)
    },
    changeDelete(param) {
      this.deleteState = param
    },
    editSuccess() {
      this.getData()
    },
    deleteThis() {
      this.deleteLoading = true
      const id = this.id
      deleteBlackList(id)
        .then(res => {
          this.deleteLoading = false
          if (res.success) {
            this.$message.success(this.$t('common.tip.deleteSuccess'))
            this.changeDelete(false)
            this.getData()
          } else {
            this.$message.error(res.message)
          }
        })
        .catch(err => {
          this.deleteLoading = false
          this.$message.error(err.message)
        })
    }
  }
}
</script>

<style lang="scss" scoped>
.el-card{
  margin: 10px;
}
#file{
  position:absolute;
  right: 0px;
  top: 0px;
  opacity: 0;
  -ms-filter: 'alpha(opacity=0)';
}
</style>
